package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.LinkedList;
import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //1.根据toUserId自己这个用户，根据score这个缘分值排序，获取第一条最高的
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);

        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);

        //调用mongoTemplate查询
        return mongoTemplate.findOne(query, RecommendUser.class);
    }


    //分页查询
    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //1.构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2.构建query对象
        Query query = Query.query(criteria);
        //3.查询总数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4.查询数据列表
        query.with(Sort.by(Sort.Order.desc("score"))).limit((page - 1) * pagesize)
                .skip(pagesize);

        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        //5.构造返回值
        return new PageResult(page, pagesize, count, list);
    }


    @Override
    public RecommendUser queryByUserId(Long userId, Long toUserId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        //用户的推荐数据
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        //如果为空，构造一个默认的
        if (user == null) {
            user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            //构建缘分值
            user.setScore(90d);
        }
        return user;
    }


    /**
     * 1.排除喜欢，不喜欢用户
     * 2.随机展示
     * 3.指定数量
     */

    @Override
    public List<RecommendUser> queryCardsList(Long userId, int counts) {
        //1.查询喜欢不喜欢的用户id
        //根据userId查询
        List<UserLike> likeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        //只需要likeUserId，提取likeUserId的字段
        List<Long> likeUserIds = CollUtil.getFieldValues(likeList, "likeUserId", Long.class);

        //2.构造查询推荐用户的条件 ，排除likeUserIds
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);

        //3.使用统计函数，随机获取推荐的用户列表、
        //newAggregation 参数：1.操作数据类型 2.设置统计参数，统计条件
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                //指定查询条件
                Aggregation.match(criteria),
                Aggregation.sample(counts)
        );
        //mongoTemplate.aggregate进行随机获取
        //results包含查询的数据 统计详情
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation,
                RecommendUser.class);

        //4.构造返回
        //只需要封装的数据列表
        return results.getMappedResults();
    }
}
